package com.lovo.sgproj.dao.imp;

import com.lovo.sgproj.bean.ClassBean;
import com.lovo.sgproj.dao.ClassDAO;
import com.lovo.sgproj.util.DBUtil;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

@Component
public class ClassDAOImp implements ClassDAO {
    @Override
    public ArrayList<ClassBean> findAllClass() {
        ArrayList<ClassBean> classLst = new ArrayList<>();
        String sql = "select pk_classid,f_classname,f_classdate,f_teacher from t_class";
        try (Connection con = DBUtil.getConnection();
             Statement st = con.createStatement()) {
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                ClassBean myClass = new ClassBean();
                myClass.setClassID(rs.getInt("pk_classid"));
                myClass.setClassName(rs.getString("f_classname"));
                myClass.setClassDate(rs.getDate("f_classdate"));
                myClass.setClassTeacher(rs.getString("f_teacher"));

                classLst.add(myClass);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return classLst;
    }

    @Override
    public ClassBean getClassByName(String className) {
        ClassBean myClass = new ClassBean();
        String sql = "select pk_classid,f_classdate,f_teacher from t_class where f_classname=?";
        try (Connection con = DBUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setString(1, className);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                myClass.setClassID(rs.getInt("pk_classid"));
                myClass.setClassDate(rs.getDate("f_classdate"));
                myClass.setClassTeacher(rs.getString("f_teacher"));
                return myClass;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insertClass(ClassBean theClass) {
        String sql = "insert into t_class (f_classname,f_classdate,f_teacher) values (?,?,?)";
        try (Connection con = DBUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setString(1, theClass.getClassName());

/*            long time = System.currentTimeMillis();
            Date date = new Date(time);
            ps.setDate(2, date);*/

            //https://www.cnblogs.com/zhaotiancheng/p/6413067.html
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM--dd");
            java.sql.Date timePara = null;
            try {
                timePara = new java.sql.Date(theClass.getClassDate().getTime());
                //System.out.println(timePara);

                ps.setDate(2, timePara);
                ps.setString(3, theClass.getClassTeacher());

                ps.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteClassByID(int classID) {
        String sql = "delete from t_class where pk_classid = ?";
        try (Connection con = DBUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setInt(1, classID);
            ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
